import Vue from 'vue';
import Toast from 'vue2-toast';
import Router from 'vue-router';
import website from '../api/website';
import cityGet from '../api/city';
import store from './../store/store'


Vue.use(Router);
Vue.use(Toast);

const Index = resolve => require(['../components/index'], resolve);
const houseList = resolve => require(['../components/houseList'], resolve);
const detail = resolve => require(['../components/detail'], resolve);
const news = resolve => require(['../components/news'], resolve);
const city = resolve => require(['../api/city'], resolve);
const base = resolve => require(['../components/base'], resolve);
const landList = resolve =>require(['../components/landList'],resolve);
const landDetail= resolve =>require(['../components/landDetail'],resolve);
const entrust= resolve =>require(['../components/entrust'],resolve);
const need= resolve =>require(['../components/need'],resolve);
const login = resolve => require(['../components/login'], resolve);




const router = new Router({
    mode: 'history',

    routes: [
        {
            path: '/',
            name: 'index',
            component: Index
        },
		{
            path: '/houseList/:type/:city',
            name: 'houseList1',
            component: houseList
        },
        {
            path: '/houseList/:type',
            name: 'houseList',
            component: houseList
        },
        {
            path:'/login',
            name:'login',
            component:login
        },
        {
            path: '/entrust',
            name: 'entrust',
            component: entrust
        },
        {
            path: '/need',
            name: 'need',
            component: need
        },
        {
            path: '/detail/:id',
            name: 'detail',
            component: detail
		},
		{
			path:'/landList',
			name:'landList',
			component:landList	 //土地
		},
		{
			path:'/landDetail/:id',
			name:'landDetail',
			component:landDetail //土地详情
		},
        {
            path: '/news/detail/:id',
            name: 'news',
            component: news
        },
        {
            path: '/base/:park_id/:id',
            name: 'base',
            component: base
        },
    ]
});
router.beforeEach((to, from, next) => {

    Vue.prototype.$loading('数据加载中');

    let cityId;
    if(to.params.city!=undefined){
        cityId = to.params.city
    }else{
         cityId = localStorage.getItem('id');
    }

	if(to.params.city!=undefined){
		store.dispatch('city',{cityId:cityId})
		//store.commit('city',cityId)
	}
	if(!store.state.city.city_id&&to.params.city==undefined){
		store.dispatch('defaultCity')
	}
	//默认城市
	

    // website.detail().then(res => {
    //     document.title = res.data.title;
    //     document.querySelector('meta[name="keywords"]').setAttribute('content', res.data.key);
    //     document.querySelector('meta[name="description"]').setAttribute('content', res.data.desc);
    // });

 

    next();
});
router.afterEach((to, from) => {
    setTimeout(function () {
        Vue.prototype.$loading.close();
    }, 500)
});


export default router;
